function display_flow_feature_on_img(src,tuv,space,frames,cc,colormap_type,minrs,maxrs)
% function display_flow_feature_on_img(src,tuv,space,frames,colormap_type,minrs,maxrs,cc)
% Display flow field features, and color them according to the singular
% energy.
%       src - the background source image in RGB (M*N*3)
%       tuv - the flow field data. A  M*N*2 matrix
%       space - grid step-size of quiver function, must be natural number
%       frames - frames of singular patterns extracted by flow_ip_detect.m
%       optional:
%             cc  - color code of the arrows
%             colormap_type - 'log' or 'linear'
%             minrs, maxrs - range of the singular energy
%             
%       
%
%       Wei Liu @ Florida Tech, 2010
%
if(nargin<4)
    error('at least four inputs are expected')
end

if(nargin<5)
    cc='y';
end

if(nargin<=6)
    colormap_type='log';
end


imshow(src,'truesize'); %paint the background
%show(src);

tsrc=imresize(src,1/space); % shrink the image
[theight twidth tnc]=size(tsrc);
[xx yy]=meshgrid(1:twidth,1:theight);
xx=xx*space;
yy=yy*space;
hold on;
spaced_quiver(tuv,space,cc,xx,yy);
hold off;
if(size(frames,2)<1)
    return;
end
hold on;
[y,I]=sort(frames(3,:));

% obtain the singular energy
rs=frames(4,:);

if(nargin<7)
    minrs=min(rs);maxrs=max(rs);
end
% colormap jet;
% cm=colormap('jet');
% colormap gray;
% cm=colormap('gray');

space=1;
%% create the color map according to the responses of the features.
for ii=1:size(frames,2)
    tt=I(ii);
    radius=frames(3,tt);
    %rs=log(frames(4,tt));
    rs=frames(4,tt);
    x=frames(1,tt);
    y=frames(2,tt);
    x=x/space;
    y=y/space;
    radius=radius/space;   
%     if(strcmp(colormap_type,'linear'))
%         c=linear_color_map(rs,minrs,maxrs,cm);
%         %c=linear_color_map(rs,minrs,maxrs,colormap('jet'));
%     else
%         c=log_color_map(rs,minrs,maxrs,cm);
%         %c=linear_color_map(rs,minrs,maxrs,colormap('jet'));
%     end
    %scatter(x,y,'+','MarkerEdgeColor',c);
    scatter(x,y,'r*');
    text(x+5,y,sprintf('%d',tt),'Color','b','FontSize',15);
    rectangle('Position',[x-radius,y-radius,radius*2,radius*2],'Curvature',[1,1],'EdgeColor','y','LineWidth',3);  
     %rectangle('Position',[x-radius,y-radius,1.5,1.5],'Curvature',[1,1],'EdgeColor','y','LineWidth',3); 
     %drawnow;
end
hold off;
title(['singular features with their responses as color index. mapping function: ' colormap_type]);
colorbar;
drawnow;

function [c]=linear_color_map(rs,minrs,maxrs,cm)
irs=round(((rs-minrs)/(maxrs-minrs))*size(cm,1));
if(irs<1)
    irs=1;
else
    if(irs>size(cm,1))
        irs=size(cm,1);
    end
end
c=cm(irs,:);

%
function [c]=log_color_map(rs,minrs,maxrs,cm)
irs=round(((log(rs)-log(minrs))/(log(maxrs)-log(minrs)))*(size(cm,1)-30));
if(isnan(irs))
    irs=1;
end
if(irs<1)
    irs=1;
else
    if(irs>size(cm,1))
        irs=size(cm,1);
    end
end
c=cm(irs+30,:);


